;;; -*- Mode:LISP; Package:USER; Base:10; Readtable:ZL -*-


(import '(si:atom si:signed-byte si:rplaca si:rplacd si:unspecial) 'lisp)
(export 'lisp:unspecial 'lisp)

(defsystem temp-hw
  (:pathname-default "k-sys:k;")
  ;(:module pack "k-system-packages")
  ;; Setf needs to be loaded before CS primitives.
  (:module nmacros "nmacros")
  (:module new-setf ("lambda-list" "new-setf"))
  (:module prims "cs-primitives")
  (:module imports "li-imports")
  (:module fakes "li-fakes")
  (:module hs "compiler-support")
  (:module hw ("hardware-constants" "instructions" "alu-opcodes" "floating-point-opcodes"))
  (:module vinc ("firm-definitions" "data-types"))
  ;(:readfile pack)
  (:compile-load nmacros)
  (:compile-load new-setf )             ;(:readfile pack)
  (:readfile prims ( (:fasload new-setf nmacros)))      ;(:readfile pack)
  (:readfile imports ((:readfile prims)))
  (:readfile fakes ( (:fasload new-setf) (:readfile prims)))    ;(:readfile pack)
  (:readfile hs ( (:fasload new-setf) (:readfile prims)))       ;(:readfile pack)
  (:readfile hw ( (:fasload new-setf) (:readfile prims hs)))    ;(:readfile pack)
  (:readfile vinc ( (:fasload  new-setf) (:readfile prims hs hw))))     ;(:readfile pack)


(defsystem fleabit-top
  (:pathname-default "k-sys:fleabit;")
  (:module extra "extra-stuff")
  (:module vect "expanding-vector")
  (:module util "top-util")
  (:module defs "top-defs")
  (:module processor-defs "processor-defs")
  (:module primop-defs "primop-defs")
  (:module primitives ("primitives" "hw-primitives"))
  (:module env "compiler-env")
  (:module decl "decl")
  (:module new-macros "new-macros")
  (:module setf-macros "k-sys:k;new-setf-macros")
  (:module rewrite "rewrite")
  (:module rewriters "rewriters")
;  (:module type-pred "k-sys:K;type-predicates")
  (:module types "k-sys:K;types")
  (:module hw-macros "hw-macros")
  (:module top "top")
  (:compile-load extra)
  (:compile-load vect (:fasload extra))
  (:compile-load util (:fasload extra) (:fasload extra))
  (:compile-load defs (:fasload util vect extra) (:fasload util vect extra))
  (:compile-load processor-defs (:readfile (temp-hw hw vinc))           ;pack
                                (:readfile (temp-hw hw vinc)))          ;pack
  (:compile-load primop-defs (:fasload defs util) (:fasload defs util))
  (:compile-load primitives (:fasload primop-defs processor-defs)
                            (:fasload primop-defs processor-defs))
  (:compile-load env (:fasload extra util) (:fasload extra util))
  (:compile-load decl)
  (:compile-load new-macros ((:readfile (temp-hw hw)) (:fasload (temp-hw nmacros) defs env decl))
                         ((:readfile (temp-hw hw)) (:fasload  (temp-hw nmacros) defs env decl)))
  (:compile-load setf-macros (:fasload (temp-hw new-setf))
                             ((:readfile (temp-hw prims)) (:fasload (temp-hw new-setf))))
  (:compile-load rewrite (:fasload defs util new-macros)
                         (:fasload defs util new-macros))
  (:compile-load rewriters (:fasload defs util new-macros rewrite)
                           (:fasload defs util new-macros rewrite))
;  (:readfile type-pred (:readfile (temp-hw fakes)))
  (:compile-load types (:readfile (temp-hw fakes))
                       (:readfile (temp-hw fakes)))
  (:compile-load hw-macros ((:readfile (temp-hw hw)) (:fasload defs new-macros))
                 ((:readfile (temp-hw hw))(:fasload defs new-macros)))
  (:compile-load top (:fasload extra env decl) (:fasload extra env decl)))

(defsystem fleabit-front-end
  (:pathname-default "k-sys:fleabit.front-end;")
  (:module type "type")
;  (:module envs "envs")
  (:module nodestuff "nodestuff")
  (:module front "nfront")
  (:module alpha "alpha")
  (:module compilators "compilators")
  (:module node "node")
  (:module simplify ("simplify" "simplify-call" "simplify-let" "nsimplify-y" "simplifiers" "param"))
;  (:module gen-interface "gen-interface")
;  (:module fixup "fixup")
  (:module module "module")
;  (:module analyze "analyze")
  (:compile-load type  (:fasload (fleabit-top defs)) (:fasload (fleabit-top defs)))
;  (:compile-load envs  (:fasload (fleabit-top defs)) (:fasload (fleabit-top defs)))
                          ;(:fasload envs) (:fasload envs))
  (:compile-load nodestuff (:fasload (fleabit-top defs util)) (:fasload (fleabit-top defs)))
                          ;(:fasload envs) (:fasload envs))
  (:compile-load front   (:fasload (fleabit-top defs util)) (:fasload (fleabit-top defs)))
  (:compile-load alpha (:fasload (fleabit-top defs env rewrite new-macros))
                       (:fasload (fleabit-top defs env rewrite new-macros)))
  (:compile-load compilators (:fasload alpha) (:fasload alpha))
  (:compile-load node (:fasload compilators front))
  (:compile-load simplify  (:fasload (fleabit-top defs)) (:fasload (fleabit-top defs)))
;  (:compile-load gen-interface (:fasload (fleabit-top defs) envs) (:fasload (fleabit-top defs) envs))
;  (:compile-load fixup  (:fasload (fleabit-top defs)) (:fasload (fleabit-top defs)))
;  (:compile-load module (:fasload alpha front) (:fasload alpha))
;  (:compile-load analyze (:fasload (fleabit-top defs) front) (:fasload (fleabit-top defs)))
)

(defsystem fleabit-back-end
  (:pathname-default "k-sys:FLEABIT.BACK-END;")
  (:module strategy  "STRATEGY")
  (:module live      "LIVE")
  (:module trace     "TRACE")
; (:module close     "CLOSE-ANALYZE")
  (:module env       "ENV-ANALYZE")
  (:compile-load strategy (:fasload (fleabit-top defs primitives)) (:fasload (fleabit-top defs primitives)))
  (:compile-load live (:fasload (fleabit-top defs) strategy) (:fasload (fleabit-top defs) strategy))
  (:compile-load trace (:fasload (fleabit-top defs primitives)) (:fasload (fleabit-top defs primitives)))
; (:compile-load close (:fasload (fleabit-top defs) strategy) (:fasload (fleabit-top defs) strategy))
  (:compile-load env (:fasload (fleabit-top defs) strategy) (:fasload (fleabit-top defs) strategy)))


(defsystem fleabit-generate
  (:pathname-default    "k-sys:FLEABIT.GENERATE;")
  (:module emit         "EMIT")
  (:module reg          "REG-ALLOC")
  (:module generate     "GENERATE")
  (:module assemble     "ASSEM")
  (:module disassemble ("DIS" "LINKER")) ;"LOADER"
  (:compile-load emit ((:fasload (fleabit-top defs util processor-defs))
                       (:readfile (temp-hw hw)))                ;pack
                      ((:fasload (fleabit-top defs processor-defs))
                       (:readfile (temp-hw hw))))               ;pack
  (:compile-load reg
                 (:fasload (fleabit-top defs util processor-defs) (fleabit-back-end strategy) emit)
                 (:fasload (fleabit-top defs processor-defs)      (fleabit-back-end strategy) emit))
  (:compile-load generate
                 (:fasload (fleabit-top util processor-defs) emit reg)
                 (:fasload (fleabit-top processor-defs)      emit reg))
  (:compile-load assemble
                 ((:fasload (fleabit-top defs processor-defs))
                  (:readfile (temp-hw hw)))
                 ((:fasload (fleabit-top defs processor-defs))
                  (:readfile (temp-hw hw))))
  (:compile-load-init disassemble (assemble)
                      ((:fasload (fleabit-top defs processor-defs))
                       (:fasload assemble)
                       (:readfile (temp-hw hw)))
                      ((:fasload (fleabit-top defs processor-defs))
                       (:fasload assemble)
                       (:readfile (temp-hw hw)))))

(defsystem compiler-crock            ;;Could we merge this into TEMP-HW?
  (:pathname-default  "k-sys:k;")
  (:module crock      "compiler-crock0")
  (:readfile crock    ((:fasload (fleabit-top defs env rewrite new-macros)
                                 (fleabit-front-end node)
                                 (fleabit-back-end env)
                                 (fleabit-generate disassemble))
                       (:readfile (temp-hw vinc hw)))))

(defsystem fleabit
  (:component-systems temp-hw fleabit-top fleabit-front-end fleabit-back-end fleabit-generate compiler-crock))
